package cn.edu.sgu.www.mhxysy.service.zuoqi.impl;

import cn.edu.sgu.www.mhxysy.entity.wenshi.Wenshi;
import cn.edu.sgu.www.mhxysy.entity.wenshi.ZuoqiWenshi;
import cn.edu.sgu.www.mhxysy.mapper.wenshi.ZuoqiWenshiMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.zuoqi.ZuoqiDTO;
import cn.edu.sgu.www.mhxysy.entity.zuoqi.Zuoqi;
import cn.edu.sgu.www.mhxysy.entity.zuoqi.ZuoqiSkill;
import cn.edu.sgu.www.mhxysy.enums.InlayStatus;
import cn.edu.sgu.www.mhxysy.enums.OpenStatus;
import cn.edu.sgu.www.mhxysy.mapper.wenshi.WenshiMapper;
import cn.edu.sgu.www.mhxysy.mapper.zuoqi.ZuoqiMapper;
import cn.edu.sgu.www.mhxysy.mapper.zuoqi.ZuoqiSkillMapper;
import cn.edu.sgu.www.mhxysy.pager.zuoqi.ZuoqiPager;
import cn.edu.sgu.www.mhxysy.service.zuoqi.ZuoqiService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ZuoqiServiceImpl implements ZuoqiService {

	private final ZuoqiMapper zuoqiMapper;
	private final WenshiMapper wenshiMapper;
	private final ZuoqiSkillMapper zuoqiSkillMapper;
	private final ZuoqiWenshiMapper zuoqiWenshiMapper;

	@Autowired
	public ZuoqiServiceImpl(
			ZuoqiMapper zuoqiMapper,
			WenshiMapper wenshiMapper,
			ZuoqiSkillMapper zuoqiSkillMapper,
			ZuoqiWenshiMapper zuoqiWenshiMapper) {
		this.zuoqiMapper = zuoqiMapper;
		this.wenshiMapper = wenshiMapper;
		this.zuoqiSkillMapper = zuoqiSkillMapper;
		this.zuoqiWenshiMapper = zuoqiWenshiMapper;
	}

	@Override
	public void insert(ZuoqiDTO zuoqiDTO) {
		Zuoqi zuoqi = toEntity(zuoqiDTO);

		zuoqi.setId(zuoqi.uuid());

		if (zuoqi.getOpenStatus() == null) {
			zuoqi.setOpenStatus(OpenStatus.UN_OPEN.getValue());
		}

		zuoqiMapper.insert(zuoqi);
	}

	@Override
	public void deleteById(String zuoqiId) {
		/*
		 * 1、删除已存在的坐骑技能
		 */
		QueryWrapper<ZuoqiSkill> queryWrapper = new QueryWrapper<>();

		queryWrapper.eq("zuoqi_id", zuoqiId);

		Long count = zuoqiSkillMapper.selectCount(queryWrapper);

		if (count > 0) {
			zuoqiSkillMapper.delete(queryWrapper);
		}

		/*
		 * 2、删除坐骑
		 */
		zuoqiMapper.deleteById(zuoqiId);

		/*
		 * 3、重置镶嵌在坐骑上的纹饰状态和坐骑ID
		 */
		Map<String, Object> columnMap = new HashMap<>();

		columnMap.put("zuoqi_id", zuoqiId);

		List<ZuoqiWenshi> list = zuoqiWenshiMapper.selectByMap(columnMap);

		if (!list.isEmpty()) {
			// 3.1 删除坐骑-宠物
			zuoqiWenshiMapper.deleteByMap(columnMap);

			for (ZuoqiWenshi zuoqiWenshi : list) {
				// 3.2 修改纹饰的镶嵌状态
				UpdateWrapper<Wenshi> updateWrapper = new UpdateWrapper<>();

				updateWrapper.eq("id", zuoqiWenshi.getWenshiId());
				updateWrapper.set("inlay_status", InlayStatus.WXQ.getValue());

				wenshiMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		}
	}

	@Override
	public void updateById(ZuoqiDTO zuoqiDTO) {
		ObjectUtils.requireNonNull(zuoqiDTO.getId(), CustomMessages.ID_NOT_NULL);

		Zuoqi zuoqi = toEntity(zuoqiDTO);

		zuoqiMapper.updateById(zuoqi);
	}

	@Override
	public List<Zuoqi> selectAll() {
		return zuoqiMapper.selectList(null);
	}

	@Override
	public Zuoqi selectById(String id) {
		return zuoqiMapper.selectById(id);
	}

	@Override
	public Page<Zuoqi> selectByPage(ZuoqiPager pager) {
		QueryWrapper<Zuoqi> queryWrapper = new QueryWrapper<>();
		Page<Zuoqi> page = Pager.ofPage(pager);

		queryWrapper.eq(
				pager.getCategoryId() != null,
				"category_id", pager.getCategoryId()
		);
		queryWrapper.eq(
				!StringUtils.isEmpty(pager.getRoleId()),
				"role_id", pager.getRoleId()
		);

		return zuoqiMapper.selectPage(page, queryWrapper);
	}

	private Zuoqi toEntity(ZuoqiDTO zuoqiDTO) {
		Zuoqi zuoqi = new Zuoqi();

		zuoqi.setId(zuoqiDTO.getId());
		zuoqi.setName(zuoqiDTO.getName());
		zuoqi.setRoleId(zuoqiDTO.getRoleId());
		zuoqi.setOpenStatus(zuoqiDTO.getOpenStatus());
		zuoqi.setCategoryId(zuoqiDTO.getCategoryId());

		return zuoqi;
	}

}